package com.acme.service.bank.impl;

import com.acme.domain.bank.Bank;
import com.acme.service.bank.BankCannotBeLoadedException;
import com.acme.service.bank.BankLoader;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class BankLoaderFromFile implements BankLoader {
    private final String path;

    public BankLoaderFromFile(String path) {
        this.path = path;
    }

    @Override
    public Bank load() throws BankCannotBeLoadedException {
        try (FileInputStream fis = new FileInputStream(path)) {
            try (BufferedInputStream bis = new BufferedInputStream(fis)) {
                try (ObjectInputStream ois = new ObjectInputStream(bis)) {
                    return (Bank) ois.readObject();
                }
            }
        } catch (Exception e) {
            throw new BankCannotBeLoadedException(e);
        }
    }
}